#include <iostream>
#include "ftphandler.hpp"
#include "ftppool.hpp"
#include "occipool.hpp"
#include "occihandler.hpp"
#include <string>
#include <vector>
#include <sstream>
#include <fstream>
#include <unistd.h>

using std::stringstream;
using std::ofstream;
using namespace std;
using namespace com_oocl_riley;

typedef struct {
	OcciPool *pool;
	int no;
} Param;

//unsigned int __stdcall ThreadFun(PVOID pM) {
//	FtpPool *pool = static_cast<FtpPool *>(pM);
//	FtpHandler handler(pool);
//	DataCollection data;
//	handler.getList(&data);
//	cout<<data.toString();
//	Sleep(2000);
//	return 0;
//}

void * ThreadFun1(void *pM) {
	//OcciPool *pool = static_cast<OcciPool *>(pM);
	Param *p = static_cast<Param*>(pM);
	//DataCollection *data = handler.perform("select message_location,to_char(create_time,'MM-DD-YY HH:MIAM'),sourcefile_size from dmtp_auditrecord where create_time > sysdate -1");
	DataCollection *data = nullptr;
	{
		OcciHandler h(p->pool);
		data =
				h.perform(
						"select ENAME,to_char(HIREDATE,'MM-DD-YY HH:MIAM'),SAL from emp");
	}
	stringstream ss;
	string no;
	ss << p->no;
	ss >> no;
	ofstream of(string("output/test") + no + ".txt", ofstream::binary);
	string resultStr = data->toString();
	//    std::cout << resultStr << std::endl;
	of.write(resultStr.c_str(), resultStr.length());
	of.close();
	delete (data);
	return 0;
}

int main() {
	//FtpHandler *fHandler = new FtpHandler("ftp://localhost", "riley", "gklmz");
	//STARTUPINFO	si;
	//PROCESS_INFORMATION pi;
	//LPTSTR szCmdLine = TEXT("DataModule.exe");
	//ZeroMemory(&si, sizeof(si));
	//si.cb = sizeof(si);
	//ZeroMemory(&pi, sizeof(pi));
	//CreateProcess(NULL, szCmdLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
	//CreateMutex(NULL, FALSE, LPCTSTR(TEXT("FtpEnv")));
	//HANDLE ftpEnv = CreateMutex(NULL, FALSE, TEXT("Global\\FtpEnv"));
	//FtpPool *pool = new FtpPool("ftp://localhost", "", "", "", 5);
	//cout << "Job Started" << endl;
	//const int NUM = 20;
	//HANDLE handle[NUM];
	//for (int i = 0;i < NUM;i++)
	//	handle[i] = (HANDLE)_beginthreadex(NULL, 0, ThreadFun, pool, 0, NULL);
	//WaitForMultipleObjects(NUM, handle, true, INFINITE);
	//cout << "Job Done" << endl;
	//for (int i = 0;i < NUM;i++)
	//	CloseHandle(handle[i]);
	//CloseHandle(ftpEnv);

	//string occiConn = "(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=hkln287p)(PORT=1521)))(CONNECT_DATA=(SID=wmssit1)))";
	//string usr = "aeowner";
	//string pwd = "aeowner";

	string occiConn =
			"(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=win2008r2)(PORT=1521)))(CONNECT_DATA=(SID=orcl)))";
	string usr = "scott";
	string pwd = "gklmz";

	OcciPool *pool = new OcciPool(occiConn, usr, pwd, 5, 1, 1);
	cout << "Job Started" << endl;
	const int NUM = 1000;
	for (int i = 0; i < NUM; i++) {
		Param *pM = new Param;
		pM->pool = pool;
		pM->no = i + 1;
		pthread_t td;
		pthread_create(&td, NULL, ThreadFun1, pM);
		pthread_join(td, nullptr);
	}
	cout << "Job Done" << endl;
	//WaitForSingleObject(pi.hProcess, INFINITE);
	//CloseHandle(pi.hProcess);
	//CloseHandle(pi.hThread);
	return 0;
}
